package com.example.shop.controller;

import com.alibaba.fastjson.JSON;
import com.example.shop.model.Commodity;
import com.example.shop.model.OrderForm;
import com.example.shop.model.User;
import com.example.shop.service.CommodityService;
import com.example.shop.service.OrderService;
import com.example.shop.service.UserService;
import com.example.shop.utils.TokenUtil;
import com.example.shop.utils.UserToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据控制器
 */
//@CrossOrigin(origins = "http://localhost:8080", maxAge = 3600, allowCredentials = "true")
//@CrossOrigin(allowCredentials = "true")
@CrossOrigin(origins = "*")
@RestController
public class DataController {
//    @Autowired
//    private DataSource dataSource;

    @Autowired
    private CommodityService commodityService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;
    private OrderForm orderForm;

    @RequestMapping("/register")
    public String register(@RequestBody User user) {
        user.setCreated(new Date());
        if (user.getRoleId() == null) {
            user.setRoleId(2);
        }
        Map<String, String> map = new HashMap<>();
        int register = userService.register(user);
        if (register != 0) {
            map.put("message", "成功");
        } else {
            map.put("error", "失败");
        }
        return JSON.toJSONString(map);
    }
    @RequestMapping("/login")
    public String login(@RequestBody User user) {
        User login = userService.login(user);
        Map<String, Object> map = new HashMap<>();
        UserToken userToken = new UserToken();
        if (login != null) {
            login.setHeadPic("http://localhost:9090/file/head_pic/" + login.getHeadPic());
            userToken.setUser(login);
            userToken.setToken(TokenUtil.createToken(login));
            map.put("message", "成功");
            map.put("user", userToken);
        } else {
            map.put("error", "失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 这个方法不需要了
     */
    @RequestMapping("/getUser")
    public String selLoginUser() {
        Map<String, Object> map = new HashMap<>();
//        if (user != null) {
//            map.put("message", "成功");
//            map.put("user", user);
//        } else {
//            map.put("message", "失败");
//        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/foods")
    public String getFoods() {
        List<Commodity> commodities = commodityService.getCommodities();
        Map<String, Object> map = new HashMap<>();
        map.put("foods", commodities);
        return JSON.toJSONString(map);
    }

    /**
     * 这个方法不需要了
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        Map<String, String> map = new HashMap<>();
        map.put("message", "/foodCity");
        return JSON.toJSONString(map);
    }

    @RequestMapping("/updateInfo")
    public Map<String, Object> updateInfo(@RequestBody User user) {
        Map<String, Object> map = new HashMap<>();
        System.err.println(user);
        int count = userService.updateInfo(user);
        if (count > 0) {
            map.put("message", "成功");
        } else {
            map.put("message", "失败");
        }
        return map;
    }
}
